package com.game.miniprogram;

import java.io.*;

/**
 * 文本换行工具，使用IDEA上班摸鱼看小说必备
 * 结合插件thief-book-idea使用
 */
public class Txt {
    public static void main(String[] args) {

        String inputFilePath = "C:\\Users\\Administrator\\Desktop\\工作文件夹\\小说\\天龙八部.txt"; // 输入文件路径
        String outputFilePath = "C:\\Users\\Administrator\\Desktop\\工作文件夹\\小说\\天龙八部2.txt"; // 输出文件路径

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            String line;
            while ((line = reader.readLine()) != null) {
                // 去除空白行：如果行是空的或者只包含空白字符，则跳过
                if (line.trim().isEmpty()) {
                    continue;
                }

                if (line.length() > 70) {
                    // 处理长行，每70个字符插入换行符
                    StringBuilder processedLine = new StringBuilder();
                    for (int i = 0; i < line.length(); i += 70) {
                        if (i + 70 < line.length()) {
                            processedLine.append(line, i, i + 70).append(System.lineSeparator());
                        } else {
                            processedLine.append(line.substring(i));
                        }
                    }
                    writer.write(processedLine.toString());
                } else {
                    // 直接写入短行
                    writer.write(line);
                }
                writer.newLine(); // 添加换行符
            }
            System.out.println("转化成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
